﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Messaging;

using WcfCore;

namespace WcfClientHost
{
	public class PerformanceTestWrapper : IDisposable
	{
		public PerformanceTestWrapper(WcfTestInput input)
		{
			_input = input;
			CreateMessageQueue();
		}

		private readonly WcfTestInput _input;
		public WcfTestInput Input
		{
			get
			{
				return _input;
			}
		}

		#region IDisposable Members
		private bool _disposed;
		private void Dispose(bool disposing)
		{
			if (_disposed)
			{
				return;
			}

			if (disposing)
			{
				// Cleanup managed
			}

			// Cleanup unmanaged
			DeleteMessageQueue();

			_disposed = true;
		}

		public void Dispose()
		{
			Dispose(true);
			GC.SuppressFinalize(this);
		}

		~PerformanceTestWrapper()
		{
			Dispose(false);
		}
		#endregion

		private MessageQueue _messageQueue;
		private void CreateMessageQueue()
		{
			if ((Input != null) && (WcfTestInput.MsmqQueueName != null) && (WcfTestInput.MsmqQueueName.Length > 0))
			{
				if (!MessageQueue.Exists(WcfTestInput.MsmqQueueName))
				{
					_messageQueue = MessageQueue.Create(WcfTestInput.MsmqQueueName, Input.Transactional);
				}
			}
		}

		private void DeleteMessageQueue()
		{
			if ((Input != null) && (WcfTestInput.MsmqQueueName != null) && (WcfTestInput.MsmqQueueName.Length > 0))
			{
				MessageQueue.Delete(WcfTestInput.MsmqQueueName);
			}
		}
	}
}
